package com.gongpingjialoan.utility;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.gongpingjialoan.activity.loans.AuditResultActivity;
import com.gongpingjialoan.activity.loans.CompleteVerificationActivity;
import com.gongpingjialoan.activity.loans.ImprovementInformationActivity;
import com.gongpingjialoan.activity.loans.LoanVerificationActivity;
import com.gongpingjialoan.activity.loans.RealNameinformationActivity;
import com.gongpingjialoan.bean.LoanStatusData;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import static com.alibaba.sdk.android.feedback.impl.FeedbackAPI.mContext;

/**
 * Created by Administrator on 2017/9/22.
 */

public class LoanUtil {
    public static String STATUS_UNAUTHED = "10"; //已登录但未完成实名认证
    public static String STATUS_NO_QUESTIONNAIRE = "20"; //未完成调查问卷
    public static String STATUS_NO_PHOTO = "30"; //未提交身份证
    public static String STATUS_INREVIEW = "40"; //提交的证件正在审核中
    public static String STATUS_PHOTO_FAILED = "50"; //提交的证件审核未通过
    public static String STATUS_PHOTO_FAILED_2 = "55"; //提交的证件审核未通过(隐藏重新上传按钮)
    public static String STATUS_PHOTO_SUCCESS = "60"; //提交的证件已完成审核

    public static void toLoanData(String json){
        try {
            JSONObject jo = new JSONObject(json);
            if (jo == null){
                return;
            }
            if ("error".equals(jo.getString("status"))){
                Toast.makeText(mContext,jo.getString("msg"),Toast.LENGTH_SHORT).show();
                return;
            }
            JSONObject data_jo = JSONUtil.getJSONObject(jo, "data");
            if (data_jo != null) {
                Gson gson = new Gson();
                LoanStatusData.setInstance(gson.fromJson(data_jo.toString(), LoanStatusData.class));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转对应页面
     * @param context
     */
    public static void goToPerfect(Context context){
        Intent it = new Intent();
        String status = LoanStatusData.getInstance().getStatus();
        if (STATUS_UNAUTHED.equals(status)){
            it.setClass(context,RealNameinformationActivity.class);
        }else if(STATUS_NO_QUESTIONNAIRE.equals(status)){
            it.setClass(context,ImprovementInformationActivity.class);
        }else if(STATUS_NO_PHOTO.equals(status)){
            it.setClass(context,AuditResultActivity.class);
        }else if(STATUS_INREVIEW.equals(status)){
            it.setClass(context,LoanVerificationActivity.class);
        }else if(STATUS_PHOTO_FAILED.equals(status)){
            it.setClass(context,CompleteVerificationActivity.class);
            it.putExtra("status",status);
        }else if(STATUS_PHOTO_FAILED_2.equals(status)){
            it.setClass(context,CompleteVerificationActivity.class);
            it.putExtra("status",status);
        }else if(STATUS_PHOTO_SUCCESS.equals(status)){
            it.setClass(context,CompleteVerificationActivity.class);
            it.putExtra("status",status);
        }else {

        }

//        it.setClass(context,AuditResultActivity.class);
        context.startActivity(it);
    }


    /**
     * 额度算法：
     定义：
     A:问卷完成每个选项填写之后计算的额度      B：用户预算    C:屏幕上方显示额度
     若意向车型为空或预算为不限，B=0;         若问卷信息为空，A=0;
     ● 当A>B时，C=A；
     ● 当A=0且B=0时，C=50,000；
     ● 当A=0且B！=0时；              若B<=50000，C=50000;      若B>50000，C=B*0.8;
     ● 当A<=B(且A.B不均为0）时；     若B<=2A,C=B*0.8;          若B>2A,C=2A;
     ● 以上,C<=500,000;
     */
    public static int computingAmount (int a,int b){
        int c = 0;

        if (a==0 && b==0){
            c = 50000;
        }else if (a == 0 && b!=0){
            if (b <= 50000){
                c = 50000;
            }else {
                c = ArithUtil.mul(b,0.8).intValue();
            }
        }else if (a > b){
            c = a;
        }else if(a <= b){
            if (b <= (2 * a)){
                c = ArithUtil.mul(b,0.8).intValue();
            }else {
                c = 2 * a;
            }
        }
        if (c > 500000){
            c = 500000;
        }


        return  c;
    }
}
